<?php

declare(strict_types=1);

namespace App\Controller\delivery;
use App\Controller\BaseController;
use App\Model\Applet;
use App\Model\AppletCompany;
use App\Model\DataSpreadPlatform;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Model\Company;
use App\Service\delivery\CompanyService;
use App\Common\Response\SystemCode;

#[AutoController]
class CompanyController  extends BaseController
{

    #[Inject]
    private CompanyService $companyService;
    /**
     * 公司列表
     */

    public function getApplet(int $spread_platform_id=0)
    {
        $userInfo = $this->companyService->getUserAppletInfo(2);
        $company_id = $userInfo['company_id'];
        $companyInfo = Company::query()->where(['id' => $company_id])->first();
        if (empty($companyInfo)) {
            return $this->response(SystemCode::SYSTEM_ERROR, '公司不存在');
        }
        $appletIds = AppletCompany::query()->where(['company_id' => $companyInfo->id])->pluck('applet_id');
        $query = Applet::query()->whereIn('id',$appletIds)->select('id as value','name as label','type');
        //20240116 新增投放平台
        if(!empty($spread_platform_id)){

           $dataSpreadPlatformInfo =  DataSpreadPlatform::query()->where('id',$spread_platform_id)->first();

           if(!empty($dataSpreadPlatformInfo)){

               $applet_type = $dataSpreadPlatformInfo->applet_type;
               $applet_typeArr = explode(',',$applet_type);
               $query->whereIn('type',$applet_typeArr);
           }
        }

        $selectData = $query->get();
        return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$selectData);

    }
}